<template>
	<view class="content">
		<okingtz-cropper saveButtonText="保存"  @uploadSuccess="uploadSuccess" :original="false"   :image="imgUrl" :maxCropper="false" :quality="1"></okingtz-cropper>
	</view>
</template>

<script>
	//  1.引入项目
	import OkingtzCropper from '@/uni_modules/okingtz-cropper/components/okingtz-cropper/okingtz-cropper'
	export default {
		components:{
			 //2.使用组件
			OkingtzCropper
		},
		data() {
			return {
				title: '上传头像示例 - 有默认图',
				imgUrl:""
			}
		},
		onLoad(data) {
			if(data.imgUrl){
				this.imgUrl = data.imgUrl
			}
		},
		methods: {
			// 3.定义自己的回调函数
			uploadSuccess(tempFilePath){
				uni.showToast({
					title:'保存成功'
				})
				// 4.根据自己的业务场景处理tempFilePath ;接口保存，或者上传至云空间
				console.log('tempFilePath_->',tempFilePath)
				
				uni.uploadFile({
					url:this.$util.BASE_URL + '/ossFile/uploadUserImg',
					filePath: tempFilePath,
					name: 'file',
					header: {
						"Authorization": uni.getStorageSync(this.$util.USER_TOKEN_KEY)
					},
					success: res=> {
						const data = JSON.parse(res.data);
						
						if(data.code == this.$ResponseStatus.OK){
							this.$api.prePage().userInfo.wxHeadimgurl = data.data;
							uni.setStorageSync(this.$util.USER_INFO,this.$api.prePage().userInfo)
							uni.navigateBack({})
						}else {
							this.$util.showInfoToast(data.message);
						}
					},
					fail: () => {
						networkError()
					},
					complete: () => {
						uni.hideLoading()
					}
				});
				
				// uni.saveImageToPhotosAlbum({
				// 	filePath:tempFilePath,
				// 	success:function(){
				// 		console.log('保存成功');
				// 	}
				// })
			}
		}
	}
</script>

<style lang="scss">

</style>
